package com.example.myapplication.util;

import com.example.myapplication.found.model.Fenxi0;
import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author: rongfeng
 * @date: 2021/8/31
 * @description
 */
public class CreatAnime {

    public static void main(String[] args) {
        String path = "C:\\Users\\demo\\Downloads\\精选-直播中-删帧版";
        String fileName = "anim_head_living.xml";

        File file = new File(path);
        File[] list = file.listFiles();

        int duration = 1000 / list.length;
        String str = "    <item\n" +
                "        android:drawable=\"@mipmap/%s\"\n" +
                "        android:duration=\"" + duration + "\" />";

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<animation-list xmlns:android=\"http://schemas.android.com/apk/res/android\">\n");

        // 如果目录下文件存在
        if (file.exists() && file.isDirectory()) {
            for (int i = 0; i < list.length; i++) {
                //取文件名子存入name中
                String name = list[i].getName();
                name = name.replace(".png", "");
                String item = String.format(str, name);
                stringBuilder.append(item + "\n");
            }
        }
        stringBuilder.append("</animation-list>");
        saveFile(stringBuilder.toString(), path + "\\" + fileName);

        System.out.println(stringBuilder.toString());
    }

    public static void saveFile(String string, String path) {
        try {
            File file = new File(path);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            // 遍历写入
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(string);
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
